//
//  NnnerCalmtrn.swift
//  HIextractLicve
//
//  Created by HIextractLicve on 2024/12/11.
//

import UIKit
import MJRefresh
import ProgressHUD

class NnnerCalmtrn: UIViewController {
    var mindfulness:Set<Int>?
    var suggestion:Set<String>?
    var  professionals:Set<CGFloat>?
    var exchangeHTLC:[String:Array<Dictionary<String,String>>]?
    var eachVioViewHTLC:THLCNOrBUTTOn?
    var mindfulnessPageHTL:THLCNPageControll?
    var journeyHTLC:UIView?
    
    var  passonHtlacData:Dictionary<String,String>
    init(stressdescHTL:Set<Int>,passonHtlacData: Dictionary<String, String>) {
        self.passonHtlacData = passonHtlacData
        super.init(nibName: nil, bundle: nil)
        mindfulness = [99]
        suggestion = ["decompression"]
        professionals = [12.1]
        exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    let FanhuiButtonHTLC = UIButton.init()
    
    let headerImgHTLC = UIImageView.init()
    let kopImgf = UIImageView.init()
    
    
    
    lazy var usernamhrHTl: THLCNPageLabl = {
        let fancelbk = THLCNPageLabl.init()
       
        fancelbk.textAlignment = .center
        
        fancelbk.textColor = .white
        fancelbk.font = UIFont(name: "Trebuchet MS Bold", size: 17)
        return fancelbk
    }()
    
    
    var maingkingNOwRoomDAtaHTLC:Array<Dictionary<String,String>>{
        let customized = "1customized0".components(separatedBy: "customized")
        if passonHtlacData["HTLisliveuser"] == customized[0] {
            return []
        }
        return [passonHtlacData]
    }
    

    lazy var seeFollowButtonHTLC: UIButton = {
        let burrton = UIButton.init()
        burrton.setImage(UIImage.init(named: "toguanzhu"), for: .normal)
        burrton.setImage(UIImage.init(named: "yijingguanzhu"), for: .selected)
        if HTLCAllDealRelaxing.Relaxing.focussuuserHTL.0.filter({$0["natureImmersion"] == passonHtlacData["natureImmersion"]}).count >= 1 {
            burrton.isSelected = true
        }
        burrton.addTarget(self, action: #selector(insetFolooweUserHTLC), for: .touchUpInside)
        return burrton
    }()
    
    lazy var sayhiButtonHTLC: UIButton = {
        let burrton = UIButton.init()
        burrton.setBackgroundImage(UIImage.init(named: "pauijgHTLC"), for: .normal)
        burrton.setImage(UIImage.init(named: "sayHichatHTL"), for: .normal)
        burrton.setTitle(" Say Hi to " + (passonHtlacData["HTLCkiName"] ?? ""), for: .normal)
        burrton.setTitleColor(.white, for: .normal)
        burrton.titleLabel?.font = UIFont(name: "Trebuchet MS Bold", size: 17.xHTL)
        burrton.addTarget(self, action: #selector(sayhitosomeoneSomentoneHTLC), for: .touchUpInside)
        return burrton
    }()
    
    
    lazy var fansCountLBLHTLC: THLCNPageLabl = {
        let fancelbk = THLCNPageLabl.init()
        fancelbk.textColor = .white
        fancelbk.textAlignment = .center
        fancelbk.font = UIFont(name: "Trebuchet MS Bold", size: 17)
   
        return fancelbk
    }()
    
    lazy var fancedesclbkLHTLC: THLCNPageLabl = {
        let fancedesclbk = THLCNPageLabl.init()
        fancedesclbk.textColor = UIColor(white: 1, alpha: 0.5)
        fancedesclbk.textAlignment = .center
        fancedesclbk.font = UIFont(name: "Trebuchet MS Bold", size: 13)
        fancedesclbk.text = "Fans"
   
        return fancedesclbk
    }()
    
    
    
    lazy var follonwerCountLBLHTLC: THLCNPageLabl = {
        let fancelbk = THLCNPageLabl.init()
        fancelbk.textColor = .white
        fancelbk.textAlignment = .center
        fancelbk.font = UIFont(name: "Trebuchet MS Bold", size: 17)

        return fancelbk
    }()
    
    lazy var folldedesclbk: THLCNPageLabl = {
        let fancedesclbk = THLCNPageLabl.init()
        fancedesclbk.textColor = UIColor(white: 1, alpha: 0.5)
        fancedesclbk.textAlignment = .center
        fancedesclbk.font = UIFont(name: "Trebuchet MS Bold", size: 13)
        fancedesclbk.text = HTLROOOMMeasg.BinrwetioBase64("Rm9sbG93")
      
        return fancedesclbk
    }()
    
    lazy var postCountLBLHTLC: THLCNPageLabl = {
        let fancelbk = THLCNPageLabl.init()
        fancelbk.textColor = .white
        fancelbk.textAlignment = .center
        fancelbk.font = UIFont(name: "Trebuchet MS Bold", size: 17)
      
        return fancelbk
    }()
    
    lazy var postdedesclbk: THLCNPageLabl = {
        let fancedesclbk = THLCNPageLabl.init()
        fancedesclbk.textColor = UIColor(white: 1, alpha: 0.5)
        fancedesclbk.textAlignment = .center
        fancedesclbk.font = UIFont(name: "Trebuchet MS Bold", size: 13)
        fancedesclbk.text = HTLROOOMMeasg.BinrwetioBase64("UG9zdA==")
        
        return fancedesclbk
    }()
 
    
  
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        usernamhrHTl.text =  passonHtlacData["HTLCkiName"]
        headerImgHTLC.image = UIImage(named: passonHtlacData["HTLCAvater"] ?? "defaullltericon")
        kopImgf.image = UIImage(named: passonHtlacData["HTLCAvater"] ?? "defaullltericon")
        fansCountLBLHTLC.text = passonHtlacData["HTLCFanecount"]
       
        follonwerCountLBLHTLC.text = passonHtlacData["HTLCFollowercount"]
        
        if self.navigationController?.navigationBar.isHidden == true {
            journeyHTLC?.alpha = 1
            journeyHTLC?.layer.cornerRadius = 12
        }
        journeyHTLC?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)

        eachVioViewHTLC = THLCNOrBUTTOn.init()
        eachVioViewHTLC?.setTitle("anchors", for: .normal)
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(eachVioViewHTLC ?? THLCNOrBUTTOn.init())
            view.addSubview(journeyHTLC!)
        }

        mindfulnessPageHTL = THLCNPageControll()
        mindfulnessPageHTL?.numberOfPages = 1
        mindfulnessPageHTL?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.tintColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.sizeToFit()

        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }

        if passonHtlacData["HTLisliveuser"] == "1" {
            let customized = "1customized0".components(separatedBy: "customized")
            postCountLBLHTLC.text = customized[1]
        }else{
            let customized = "1customized0".components(separatedBy: "customized")
            postCountLBLHTLC.text = customized[0]
        }
        
    }
    
    lazy var builCollectionLiveViewHTL: UICollectionView = {
        let coijfLayout = UICollectionViewFlowLayout.init()
        coijfLayout.itemSize = CGSize.init(width: (UIScreen.main.bounds.width - 24.xHTL), height:320)
        
        coijfLayout.minimumInteritemSpacing = 12
        coijfLayout.scrollDirection = .vertical
        coijfLayout.minimumLineSpacing = 24
        
        
        let fiettCol = UICollectionView.init(frame: .zero, collectionViewLayout: coijfLayout)
        let nib = UINib(nibName: "MindfulnessPracticeLL", bundle: nil)
               
        fiettCol.register(nib, forCellWithReuseIdentifier: "HTLRDymmmlostViewCellID")
             
        fiettCol.delegate = self
        fiettCol.dataSource = self
       
            fiettCol.showsVerticalScrollIndicator = false
        
        
        fiettCol.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 310, right: 0)
       fiettCol.showsVerticalScrollIndicator = false
       
        fiettCol.backgroundColor = .clear
        fiettCol.dataSource = self
        fiettCol.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                fiettCol.reloadData()
                

               
                    fiettCol.mj_header?.endRefreshing()
                
                
            }
            
        })
        return fiettCol
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(bloaaalackSomentoneRemoveHTLC), name: NSNotification.Name("htldeleteuser"), object: nil)
        
        kopImgf.contentMode = .scaleToFill
        view.addSubview(kopImgf)
        kopImgf.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0).cgColor, UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = self.view.bounds
        bgLayer1.startPoint = CGPoint(x: 0.5, y: 0)
        bgLayer1.endPoint = CGPoint(x: 0.9, y: 0.9)
        kopImgf.layer.addSublayer(bgLayer1)
        
        
        FanhuiButtonHTLC.setImage(UIImage.init(named: "graybackjHTL"), for: .normal)
        FanhuiButtonHTLC.addTarget(self, action: #selector(gogonextTonmesglistHTL(hijkBUtton:)), for: .touchUpInside)

        view.addSubview(FanhuiButtonHTLC)
        FanhuiButtonHTLC.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.leading.equalToSuperview().offset(12)
            make.top.equalToSuperview().offset(saaffeeTppoHightHTL + 8)
        }
        
        let nortyiButton = UIButton.init()
        nortyiButton.setImage(UIImage.init(named: "more_hometitle"), for: .normal)
        nortyiButton.addTarget(self, action: #selector(gogonextTonmesglistHTL(hijkBUtton:)), for: .touchUpInside)
        view.addSubview(nortyiButton)
        nortyiButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(saaffeeTppoHightHTL + 8)
            make.trailing.equalToSuperview().inset(12.xHTL)
            make.width.equalTo(38.xHTL)
            make.height.equalTo(38.xHTL)
        }
        
        view.addSubview(self.seeFollowButtonHTLC)
        seeFollowButtonHTLC.snp.makeConstraints { make in
            make.width.equalTo(84)
            make.height.equalTo(34)
            make.centerY.equalTo(nortyiButton)
            make.trailing.equalTo(nortyiButton.snp.leading).offset(-20.xHTL)
        }
       
        
        
        headerImgHTLC.contentMode = .scaleToFill
        headerImgHTLC.layer.masksToBounds = true
        headerImgHTLC.layer.cornerRadius = 22.5
         
        
        view.addSubview(headerImgHTLC)
        headerImgHTLC.snp.makeConstraints { make in
            make.top.equalTo(nortyiButton.snp.bottom).offset(70.yHTL)
            make.width.height.equalTo(70.xHTL)
            make.leading.equalToSuperview().offset(12.xHTL)
        }
        
        
        view.addSubview(usernamhrHTl)
        usernamhrHTl.snp.makeConstraints { make in
            make.leading.equalTo(headerImgHTLC.snp.trailing).offset(12)
            make.centerY.equalTo(headerImgHTLC)

        }
        
        
        let mainScrio = UIScrollView.init()
        
        view.addSubview(mainScrio)
        mainScrio.snp.makeConstraints { make in
            make.leading.bottom.trailing.equalToSuperview()
            make.top.equalTo(headerImgHTLC.snp.bottom)
        }
        
      
      
        
        let comye = (UIScreen.main.bounds.width)/3
        
      
        mainScrio.addSubview(fansCountLBLHTLC)
        mainScrio.addSubview(fancedesclbkLHTLC)
      
        mainScrio.addSubview(follonwerCountLBLHTLC)
        mainScrio.addSubview(folldedesclbk)
        
        
        mainScrio.addSubview(postCountLBLHTLC)
        mainScrio.addSubview(postdedesclbk)
        mainScrio.addSubview(sayhiButtonHTLC)
        
    
        fansCountLBLHTLC.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.width.equalTo(comye)
            make.top.equalToSuperview().offset(31)
            make.height.equalTo(22)
           
        }
        
        fancedesclbkLHTLC.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.width.equalTo(comye)
            make.top.equalTo(fansCountLBLHTLC.snp.bottom).offset(5)
            make.height.equalTo(22)
           
        }
        
        
        follonwerCountLBLHTLC.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(comye)
            make.top.equalToSuperview().offset(31)
            make.height.equalTo(22)
        }
        
        folldedesclbk.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(comye)
            make.top.equalTo(follonwerCountLBLHTLC.snp.bottom).offset(5)
            make.height.equalTo(22)
        }
        
        postCountLBLHTLC.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.width.equalTo(comye)
            make.top.equalToSuperview().offset(31)
            make.height.equalTo(22)
        }
        
        postdedesclbk.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.width.equalTo(comye)
            make.top.equalTo(postCountLBLHTLC.snp.bottom).offset(5)
            make.height.equalTo(22)
        }
        
        
        sayhiButtonHTLC.snp.makeConstraints { make in
            make.width.equalTo(332.xHTL)
            make.height.equalTo(52.xHTL)
            make.leading.trailing.equalToSuperview().inset((UIScreen.main.bounds.width - 332.xHTL)/2)
            make.top.equalTo(postdedesclbk.snp.bottom).offset(35)
        }
        
        
        let posdterlbccc = UILabel.init()
        posdterlbccc.textColor = .white
        posdterlbccc.textAlignment = .left
        posdterlbccc.font = UIFont(name: "Trebuchet MS Bold", size: 20)
        posdterlbccc.text = "Post"
        
        
        
        
        
        
        mainScrio.addSubview(posdterlbccc)
        posdterlbccc.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(12.xHTL)
            make.top.equalTo(sayhiButtonHTLC.snp.bottom).offset(36)
        }
        
      
       
        ProgressHUD.animate()
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
           
            ProgressHUD.dismiss()
            mainScrio.addSubview(self.builCollectionLiveViewHTL)
            self.builCollectionLiveViewHTL.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview().inset(12)
                make.width.equalTo(UIScreen.main.bounds.width - 24)
                make.bottom.equalToSuperview().offset(-93 - 40)
                make.top.equalTo(posdterlbccc.snp.bottom).offset(16.xHTL)
                make.height.equalTo(320)
            }
           
        }
        
       
        
    }

   @objc func bloaaalackSomentoneRemoveHTLC()  {
       self.navigationController?.popViewController(animated: true)
    }
    
    /// 拉黑举报
    /// - Parameter hijkBUtton: <#hijkBUtton description#>
    @objc func gogonextTonmesglistHTL(hijkBUtton:UIButton)  {
        if hijkBUtton == self.FanhuiButtonHTLC {
            self.navigationController?.popViewController(animated: true)
            return
        }
        
//        let kiuop = "Block****Report".components(separatedBy: "****")
        let Asvc = UIAlertController(title: HTLROOOMMeasg.BinrwetioBase64("UmVwb3J0aW5nIG9yIEJsb2Nr"), message: HTLROOOMMeasg.BinrwetioBase64("QXJlIHlvdSBzdXJlIHlvdSB3YW50IHRvIGJsb2NrIHRoaXMgdXNlcj8gU2VsZWN0IHRoZSBvcGVyYXRpb24geW91IHdhbnQgdG8gZG8uIEFmdGVyIHRoZSB1c2VyIGlzIGJsYWNrbGlzdGVkLCBhbGwgcmVsYXRlZCBpbmZvcm1hdGlvbiB3aWxsIG5vIGxvbmdlciBiZSBkaXNwbGF5ZWQhISE="), preferredStyle:.actionSheet)
        
        let ActdeleBlock = UIAlertAction(title: HTLROOOMMeasg.BinrwetioBase64("QmxvY2s="), style: .default) { acvf in
            ProgressHUD.animate()
            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
               
                ProgressHUD.succeed(HTLROOOMMeasg.BinrwetioBase64("VGhlIHVzZXIgaGFzIGJlZW4gYmxhY2tsaXN0ZWQh"), delay: 2.5)
                HTLCAllDealRelaxing.Relaxing.removeApplerlUserFrom(stressdescHTL: [3334,34,333], WhoINfoHTL:self.passonHtlacData)
               
            }
           
        }
       
       
        let reportdeleBlock = UIAlertAction(title: HTLROOOMMeasg.BinrwetioBase64("UmVwb3J0"), style: .default) { acvf in
            self.navigationController?.pushViewController(UntentionSetting.init(), animated: true)
        }
        Asvc.addAction(ActdeleBlock)
        
            Asvc.addAction(reportdeleBlock)
        
       
        self.present(Asvc, animated: true)
    }
    
    
    /// chat
    @objc func sayhitosomeoneSomentoneHTLC()  {
       
        self.navigationController?.pushViewController(TranquilState.init(stressdescHTL: [222,222], passonHtlacData: self.passonHtlacData), animated: true)
    }
    
    
    /// 关注
    @objc func insetFolooweUserHTLC()  {
        ProgressHUD.animate()
      
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0, execute: DispatchWorkItem(block: {
            
            ProgressHUD.dismiss()
           
            self.seeFollowButtonHTLC.isSelected = !self.seeFollowButtonHTLC.isSelected
            
            if HTLCAllDealRelaxing.Relaxing.focussuuserHTL.0.filter({$0["natureImmersion"] == self.passonHtlacData["natureImmersion"]}).count == 0 && self.seeFollowButtonHTLC.isSelected == true{
                
                //关注但是列表没有
                HTLCAllDealRelaxing.Relaxing.focussuuserHTL.0.insert(self.passonHtlacData, at: 0)
                return
            }
            
            
            if HTLCAllDealRelaxing.Relaxing.focussuuserHTL.0.filter({$0["natureImmersion"] == self.passonHtlacData["natureImmersion"]}).count >= 1 && self.seeFollowButtonHTLC.isSelected == false{
                for (iooo,Item) in HTLCAllDealRelaxing.Relaxing.focussuuserHTL.0.enumerated() {
                    if Item["natureImmersion"] == self.passonHtlacData["natureImmersion"] {
                        //没关注但是列表有
                        HTLCAllDealRelaxing.Relaxing.focussuuserHTL.0.remove(at: iooo)
                    }
                }
            }
           
          
          
            return
        }))
       
     }
    
   
}


extension NnnerCalmtrn:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.maingkingNOwRoomDAtaHTLC.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let passonHtlacData = maingkingNOwRoomDAtaHTLC[indexPath.row]
        let customized = "1customized0".components(separatedBy: "customized")
        let fffuarcell = collectionView.dequeueReusableCell(withReuseIdentifier: "HTLRDymmmlostViewCellID", for: indexPath) as! MindfulnessPracticeLL
        let Timrhg = (passonHtlacData["HTLLiveDYMPiclist"]  ?? "").components(separatedBy: "*")
        fffuarcell.attitudeImageHTl.image = UIImage(named: (passonHtlacData["HTLCIDIlikeThisDIM"] == customized[0]) ? "dymmeiYIzan" : "dymmeizan")
        
        fffuarcell.oneCupCoverHTLC.image = UIImage(named: Timrhg.first  ?? "")
        fffuarcell.userHeaderHTL.image = UIImage(named: passonHtlacData["HTLCAvater"]  ?? "")
        fffuarcell.usernamhrHTl.text = passonHtlacData["HTLCkiName"]
        fffuarcell.pubtimewHTl.text = passonHtlacData["HTLDYMPunoTime"]
        
        fffuarcell.dyyumTitlewHTl.text = passonHtlacData["HTLLiveDYMDesc"]
        fffuarcell.backgroundColor = .clear
        return fffuarcell
        
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let passonHtlacData = maingkingNOwRoomDAtaHTLC[indexPath.row]
       
        let enterlive = DeepBreathing.init(stressdescHTL: [456,456,456], passonHtlacData: passonHtlacData)
        self.navigationController?.pushViewController(enterlive, animated: true)
    }
    
    
}
